
<template>
  <div>
    <el-row :gutter="15">
      <el-form ref="elForm" :model="formData" :rules="rules" size="medium" label-width="100px">
        <el-col :span="22">
          <el-form-item label="作业标题" prop="title">
            <el-input
              v-model="title"
              readonly
              :maxlength="20"
              show-word-limit
              clearable
              prefix-icon="el-icon-mobile"
              :style="{width: '100%'}"
            ></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="22">
          <el-form-item label="作业内容" prop="content">
            <wangEditor v-model="formData.content"></wangEditor>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item size="large">
            <el-button type="primary" @click="submitForm">提交</el-button>
          </el-form-item>
        </el-col>
      </el-form>
    </el-row>
  </div>
</template>
<script>
import wangEditor from "@/components/wangEditor/Editor";
import { writePost, getTaskContent } from "@/api/task";
export default {
  mounted() {
    this.getContent();
  },
  data() {
    return {
      title: this.$route.query.title,
      formData: {
        id: null,
        score: null,
        sno: this.$store.state.user.id,
        taskInfoId: this.$route.query.taskId,
        content: ""
      },
      rules: {
        content: [
          {
            required: true,
            message: "请入输您的答案",
            trigger: "blur"
          }
        ]
      }
    };
  },
  components: { wangEditor },
  methods: {
    submitForm() {
      this.$refs["elForm"].validate(valid => {
        if (!valid) return;
        this.post();
      });
    },
    async getContent() {
      const data = await getTaskContent(
        this.formData.sno,
        this.formData.taskInfoId
      );
      const res = data.data.data.data;
      if (res != null) {
        this.formData.id = res.id;
        this.formData.content = res.content;
        this.formData.score = res.score;
      }
    },
    async post() {
      const { data: res } = await writePost(this.formData);
      if (res.code == 20000) {
        this.$notify.success({
          title: "操作成功",
          message: res.message
        });
        this.$router.push({ path: "/task" });
      } else {
        return this.$message.error("操作失败:" + res.message);
      }
    }
  }
};
</script>
<style scoped>
h1 {
  font-size: 2em;
}
#editor {
  margin-top: 2em;
}
</style>
